package com.hwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwy.common.CostumException;
import com.hwy.entity.Category;
import com.hwy.entity.Dish;
import com.hwy.entity.Setmeal;
import com.hwy.mapper.CategoryMapper;
import com.hwy.service.CategoryService;
import com.hwy.service.DishService;
import com.hwy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删之前判断是否关联菜品，套餐
     * @param id
     */
    @Override
    public void remove(Long id) {

        //查询是否关联菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if(dishCount > 0){
            throw new CostumException("当前分类关联了菜品,不能删除。");
        }

        //查询是否关联套餐
        LambdaQueryWrapper<Setmeal> setmaelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmaelLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int setMealCount = setmealService.count(setmaelLambdaQueryWrapper);
        if(setMealCount > 0){
            throw new CostumException("当前分类关联了套餐,不能删除。");
        }

        //既没有关联菜品，也没有关联套餐
        super.removeById(id);
    }
}
